import {createRouter, createWebHistory} from 'vue-router'
import Login from '../views/Login.vue'


const router = createRouter({

    history:createWebHistory(),
    routes:[
        {
            path: '/login',
            name: 'Login',
            component: Login
        },
        {
            path: '/home',
            name: 'Home',
            component: ()=> import("@/views/Home.vue"),
        },
        {
            path: '/about',
            name: 'About',
            // 懒加载
            component: ()=> import("@/views/About.vue"),
        },
        {
            path: '/myInfo',
            name: 'MyInfo',
            component: () => import("@/views/MyInformation.vue")
        },
        {
            path: '/userManage',
            name: 'UserManage',
            component: () => import("@/views/userManage.vue")
        },
        {
            path: '/graphGeneration',
            name: 'GraphGeneration',
            component: () => import("@/views/GraphGeneration.vue")
        },
        {
            path: '/myGraphs',
            name: 'MyGraphs',
            component: () => import("@/views/MyGraphs.vue")
        },
        {
            path:'/',
            redirect: '/login'
        }
    ]
})
// 全局导航守卫
router.beforeEach((to, from, next) => {
    // 根据路由设置背景
    if (to.path === '/login') {
        document.body.classList.add('login-background');
    } else {
        document.body.classList.remove('login-background');
    }
    // 检查登录与否,未登录就重定向到登录
    const user = JSON.parse(localStorage.getItem('user'));
    if (!user && to.path !== '/login') {
        next('/login');
    } else {
        next();
    }
});


export default router